package com.chen.forum.controller;

import com.chen.forum.Service.UserService;
import com.chen.forum.model.User;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
public class AuthorizeController {
    @Autowired
    private UserService userService;

    //请求QQ登录
    @RequestMapping("/loginByQQ")
    public void loginByQQ(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        try {
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
            System.out.println("请求QQ登录,开始跳转...");
        } catch (QQConnectException | IOException e) {
            e.printStackTrace();
        }
    }

    //QQ登录的回调方法
    @RequestMapping("/qqconnect")
    public String qqconnect(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) throws QQConnectException {

        AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
        String accessToken = null, openID = null;
        long tokenExpireIn = 0L;
        if ("".equals(accessTokenObj.getAccessToken())) {
            System.out.println("登录失败:没有获取到响应参数");
            return "accessTokenObj=>" + accessTokenObj + "; accessToken" + accessTokenObj.getAccessToken();
        } else {
            accessToken = accessTokenObj.getAccessToken();
            tokenExpireIn = accessTokenObj.getExpireIn();
            System.out.println("accessToken" + accessToken);
            request.getSession().setAttribute("demo_access_token", accessToken);
            request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));

            // 利用获取到的accessToken 去获取当前用的openid -------- start
            OpenID openIDObj = new OpenID(accessToken);
            openID = openIDObj.getUserOpenID();

            UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
            UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
            if (userInfoBean.getRet() == 0) {
                String name = removeNonBmpUnicode(userInfoBean.getNickname());
                String imgUrl = userInfoBean.getAvatar().getAvatarURL100();
                User user = new User();
                String token=UUID.randomUUID().toString();
                user.setToken(token);
                user.setName(name);
                user.setAccountId(openID);
                user.setAvatarUrl(imgUrl);
                userService.createOrUpdate(user);
                Cookie cookie = new Cookie("token", token);
                cookie.setMaxAge(14*24*60*60);
                response.addCookie(cookie);
                return "redirect:/";
            } else {
                System.out.println("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg());
            }
        }
        return "redirect:/";
    }

    //处理掉QQ网名中的特殊表情
    public String removeNonBmpUnicode(String str) {
        if (str == null) {
            return null;
        }
        str = str.replaceAll("[^\\u0000-\\uFFFF]", "");
        if ("".equals(str)) {
            str = "($ _ $)";
        }
        return str;
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request,
                         HttpServletResponse response) {
        request.getSession().removeAttribute("user");
        Cookie cookie = new Cookie("token", null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "redirect:/";
    }

}
